package com.example.administrator.waitan.homepage;

import android.content.Context;
import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.ImageView;

import com.example.administrator.waitan.R;
import com.example.administrator.waitan.homepage.recommend.RecommendActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/1/15.
 */
public class HomePagerViewPagerAdapter extends PagerAdapter {
    private Context context;
    private ArrayList<View> views;
    private HomePagerBeans beans;
    public HomePagerViewPagerAdapter(Context context) {
        this.context = context;
        addData();
    }
    public void addData( ){
        View v = LayoutInflater.from(context).inflate(R.layout.viewpager_homepager,null);
        View v1 = LayoutInflater.from(context).inflate(R.layout.viewpager_homepager_two,null);
        View v2 = LayoutInflater.from(context).inflate(R.layout.viewpager_homepager_three,null);
        views = new ArrayList<>();
        views.add(v);
        views.add(v1);
        views.add(v2);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return 3;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
//        //获取需要加载视图的父控件
//        ViewParent parent = views.get(position % views.size()).getParent();
//        //判断父控件是否为空，若为空说明已被ViewPager加载，那么我们要移除改视图，这样就能保证一个视图只有一个父控件，
//        if(parent!=null){
//            container.removeView(views.get(position % views.size()));
//        }
//        container.addView(views.get(position % views.size()));
        container.addView(views.get(position%views.size()));
        views.get(position % views.size()).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent goRecommend = new Intent(context, RecommendActivity.class);
                context.startActivity(goRecommend);
            }
        });
        return views.get(position%views.size());

    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView(views.get(position%views.size()));
    }
}
